﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Datalayer;
using Common;

namespace Portal_KonTum.Admin
{
    public partial class UserMn : System.Web.UI.Page
    {
        IQueryMgr iq;
        protected void Page_Load(object sender, EventArgs e)
        {
            notes();
            if (!IsPostBack)
            {
                bin();
            }
        }
        void notes()
        {
            if (Request.QueryString["Notes"] != null && Request.QueryString["Active"] != null)
            {
                string snotes = Request.QueryString["Notes"];
                string sactive = Request.QueryString["Active"];
                string sout = MethodCommon.StrQueryNotes(snotes, sactive);
                lbnotes.Visible = true;
                lbnotes.InnerText = sout;
            }
        }
        void bin()
        {
            List<User> ex = new List<User>();
            iq = QueryMgr.GetDbInstance();
            ex = iq.UR_GetAllUser();
            GridView1.DataSource = ex;
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HiddenField hf = (HiddenField)e.Row.FindControl("hf_role");
                Label lb_c = (Label)e.Row.FindControl("lb_role");
                iq = QueryMgr.GetDbInstance();
                Role ur = new Role();
                ur = iq.Ro_GetRoleByID(MethodCommon.Get_Guid(hf.Value));
                lb_c.Text = ur.RoleName + "";

                HiddenField hfs = (HiddenField)e.Row.FindControl("hf_status");
                Label lb_cs = (Label)e.Row.FindControl("lb_status");
                iq = QueryMgr.GetDbInstance();
                Status st = new Status();
                st = iq.Sta_GetStatusByID(MethodCommon.Get_Guid(hfs.Value));
                lb_cs.Text = st.StatusName + "";
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            iq = QueryMgr.GetDbInstance();
            User cus = new User();
            String index = e.CommandArgument.ToString();

            if (e.CommandName == "rowEdit")
            {
                Response.Redirect("UserNew.aspx?UrID=" + index);
            }
            if (e.CommandName == "rowDelete")
            {
                Guid _id = new Guid(index);
                iq = QueryMgr.GetDbInstance();
                iq.UR_DelOrResUser(_id);
                bin();
                Response.Redirect("UserMn.aspx?Notes=StrDel&Active=true");
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            bin();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;

            GridView1.EditIndex = -1;
            GridView1.SelectedIndex = -1;
        }
    }
}